package org.xlp.db.sql;

/**
 * Description: Union sql对象 默认只支持Oracle分页查询
 * <br/>date: 2024/1/16 10:59
 *
 * @version 1.0
 * @author: xlp
 */
public class OracleUnionSQL extends UnionSQL{
    private OracleUnionSQL() {}

    /**
     * 构造UnionSQL对象
     *
     * @param query 可查询SQL对象
     * @return
     * @throws NullPointerException 假如参数为null，则抛出该异常
     */
    public static OracleUnionSQL of(Query query){
        OracleUnionSQL unionSQL = new OracleUnionSQL();
        unionSQL.query = query;
        return unionSQL;
    }

    @Override
    public String getParamSql() {
        String sql = getNotLimitParamSql();
        sql = "(" + sql + ") tt_";
        sql = "(select tt_.*, rownum rn_ from " + sql + ")";
        sql = "select * from " + sql;
        if (getLimit() != null) {
            sql += " where rn_ between ? and ?";
        }
        return sql;
    }

    @Override
    public Object[] getParams() {
        Object[] values = super.getParams();
        if (getLimit() != null) {
            values[values.length - 2] = getLimit().getStartPos() + 1;
            values[values.length - 1] = getLimit().getStartPos() + getLimit().getResultCount();
        }
        return values;
    }
}
